package webapp.frontOffice.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import modele.contrat.Contrat;
import modele.gestionVente.OffreDeVente;
import modele.utilisateur.Utilisateur;
import dao.contrat.ContratDAO;
import dao.gestionVente.OffreDeVenteDAO;

/**
 * Servlet implementation class ActionAcheter
 */
@WebServlet("/action/acheter")
public class ActionAcheter extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		HttpSession session = request.getSession();
		
		if(session == null || session.getAttribute("user") == null || !session.getAttribute("type").equals("investisseur"))
		{
			response.sendRedirect("index.jsp");
		}
		else
		{
			Utilisateur user = (Utilisateur)session.getAttribute("user");
			
			long idvente = Long.parseLong(request.getParameter("idvente"));
			OffreDeVente vente = new OffreDeVenteDAO().retrieve(idvente);
			Contrat contrat = (Contrat) vente.getPortefeuille().toArray()[0];
			vente.setActive(false);
			contrat.setPossesseur(user);
			
			new OffreDeVenteDAO().update(vente);
			new ContratDAO().update(contrat);
			
	        response.sendRedirect(request.getContextPath() + "/mesactions");
		}
		
	}


}
